<?php
class kcback_cartControl extends skymvc{
	public $typeid=1;
	public function __construct(){
		parent::__construct();
	}
	
	public function onDefault(){
		$option=array(
			"where"=>" typeid=1 AND shopid=".SHOPID.""
		);
		$data=M("kcback_cart")->select($option);
		 
		if($data){
			foreach($data as $k=>$v){
				$productids[]=$v['productid'];
			}
			$products=M("product")->getListByIds($productids);
			
			foreach($data as $k=>$v){
				$v['title']=$products[$v['productid']]['title'];
					$v['price']=$products[$v['productid']]['price'];
					$v['lower_price']=$products[$v['productid']]['lower_price'];
					$v['imgurl']=$products[$v['productid']]['imgurl'];
					$v['shopname']=$shops[$v['shopid']]['title'];
				$data[$k]=$v;
			}
		}
		$this->smarty->assign(array(
			"data"=>$data
		));
		$this->smarty->display("kcback_cart/index.html");
	}
	
	public function onSaveAll(){
		$nums=post('nums','i');
		$kcid=post('kcid','i');
		$ids=post("ids",'i');
		if($nums && $ids && $kcid){
			foreach($nums as $k=>$v){
				if($v<=0) continue;
				$row=M("ymdian_product")->selectRow("id=".$ids[$k]);
				$kcproduct=M("kucun_product")->selectRow("shopid=".$kcid." AND productid=".$k);
				$product=M("product")->selectRow("id=".$k);
				if($row['total_num']<$v){
					$this->goAll($product['title']."退货数量不能大于".$kcproduct['total_num'],1);
				}
				
				 
				
				$cart=M("kcback_cart")->selectRow("shopid=".SHOPID." AND typeid=".$this->typeid." AND productid=".$k." AND object_id=".$ids[$k]." AND kcid=".$kcid."");
				if($cart){
					M("kcback_cart")->update(
							array(
								"amount"=>$v
							),"id=".$cart['id']
						);
					}else{
						M("kcback_cart")->insert(array(
							"productid"=>$k,
							"object_id"=>$ids[$k],
							"dateline"=>time(),
							"amount"=>$nums[$k],
							"shopid"=>SHOPID,
							"siteid"=>SITEID,
							"typeid"=>$this->typeid,
							"kcid"=>$kcid,
							"price"=>$product['price']
						));	
					}
			}
			header("Location: ".APPYMDIAN."?m=kcback&a=order");
		}else{
			$this->goAll("数据出错");
		}
		 
		
	}
	
}
?>